import json
# import elasticsearch
from elasticsearch import Elasticsearch

es = Elasticsearch([{"host": "39.108.176.217", "port": 9200}])
# print(elasticsearch.__version__)
# print(es.ping())

query = {
    "match": {
        "content": "搜索引擎"
    }
}

query2 = {
    "match_all": {}
}

result = es.search(index="my_index", query=query2)
print(result)
print(json.dumps(result, ensure_ascii=False, indent=4))
# {
#     "took": 34, //查询耗时
#     "timed_out": false, //是否超时
#     "_shards": {
#         "total": 1,
#         "successful": 1,
#         "skipped": 0,
#         "failed": 0
#     },
#     "hits": {
#         "total": {
#             "value": 1, //匹配文档总数
#             "relation": "eq" //数量关系：eq(精确)、gte(大于等于)
#         },
#         "max_score": 1.1507283, //最高相关性评分
#         "hits": [
#             {
#                 "_index": "my_index",
#                 "_type": "_doc",
#                 "_id": "mNrnepkBjBuDs0G4IluF", //文档唯一ID
#                 "_score": 1.1507283, //相关性评分
#                 "_source": { //原始文档内容
#                     "title": "python\u5b9e\u6218elasticsearch",
#                     "author": "zxp",
#                     "content": "elasticsearch\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u641c\u7d22\u5f15\u64ce"
#                 }
#             }
#         ]
#     }
# }
for hit in result["hits"]["hits"]:
    print(hit["_source"])